//
//  ChapterDetailViewController.m
//  Learn
//
//  Created by 迟建国 on 2024/4/2.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "ChapterDetailViewController.h"
#import "ChapterDetailTableViewCell.h"
#import "ListenTestViewController.h"
#import "TextbookChapterDetailModel.h"
#import "HomeRequest.h"
#import "BookReadTypeView.h"

#import "PlayManager.h"
#import "HearingChapterDetailViewController.h"
#import "WorkCardViewController.h"
#import "WrittenExaminationController.h"
@interface ChapterDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) TextbookChapterDetailModel *model;
@end

@implementation ChapterDetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [PlayManager sharPlayManager].useTime = 0;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.chapterName;
    self.view.backgroundColor = rgba(229, 229, 229, 1);
    [self setUI];
    [self loadData];
    // Do any additional setup after loading the view.
}

- (void)loadData {
    NSString *type = @"";
    if (self.type == BookTypeHearsay) {
        type = @"听说";
    } else if (self.type == BookTypeHearing) {
        type = @"听力";
    } else if (self.type == BookTypeFollowUpReading) {
        type = @"跟读";
    } else if (self.type == BookTypeWrittenExam) {
        type = @"笔试";
    } else if (self.type == BookTypePhonetic) {
        type = @"音标";
    } else if (self.type == BookTypeModelsp) {
        type = @"典范";
    } else if (self.type == BookTypeNewconcept) {
        type = @"新概念";
    } else if (self.type == BookTypeBlackplum) {
        type = @"黑布林";
    } else if (self.type == BookTypeInterest) {
        type = @"趣味";
    }  else {
        
    }
    [HomeRequest getTextbookChapterDetailWithProductId:self.productId type:type chapterid:self.chapterId result:^(TextbookChapterDetailModel *model) {
        self.model = model;
        [self.tableView reloadData];
    }];
}

- (void)setUI {
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,lTopBarHeight, SCREEN_W, SCREEN_H - lTopBarHeight - SafeAreaBottomHeight) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    if (@available(iOS 13.0, *)) {
        [self.tableView setAutomaticallyAdjustsScrollIndicatorInsets:NO];
    } else {
        // Fallback on earlier versions
    }
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    } else {
        // Fallback on earlier versions
    }
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    self.tableView.backgroundColor=[UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"ChapterDetailTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ChapterDetailTableViewCell"];
    if (self.type == BookTypeHearsay) {
        self.view.backgroundColor = lWhiteColor;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.type == BookTypeWrittenExam) {
        return self.model.sr.count;
    } else if (self.type == BookTypeHearing) {
        return self.model.lr.count;
    } else if (self.type == BookTypeFollowUpReading) {
        return self.model.rr.count;
    } else if (self.type == BookTypeModelsp||self.type == BookTypeNewconcept||self.type == BookTypeBlackplum) {
        return self.model.sr.count;
    } else{
        return self.model.hsr.count;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 83;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChapterDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChapterDetailTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"ChapterDetailTableViewCell" owner:self options:nil] lastObject];
    }
    if (self.type == BookTypeWrittenExam) {
        cell.model = self.model.sr[indexPath.row];
    } else if (self.type == BookTypeHearing) {
        cell.model = self.model.lr[indexPath.row];
    }  else if (self.type == BookTypeFollowUpReading) {
        cell.model = self.model.rr[indexPath.row];
    }  else if (self.type == BookTypeModelsp||self.type == BookTypeNewconcept||self.type == BookTypeBlackplum) {
        cell.model = self.model.sr[indexPath.row];
    } else {
        cell.model = self.model.hsr[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.type == BookTypeFollowUpReading) { 
        WorkCardViewController *vc = [[WorkCardViewController alloc] init];
        vc.model = self.model.rr[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    } else if (self.type == BookTypeHearing) {
        HearingChapterDetailViewController *vc = [[HearingChapterDetailViewController alloc] init];
        vc.model = self.model.lr[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    } else if (self.type == BookTypeWrittenExam) {
        WrittenExaminationController *vc = [[WrittenExaminationController alloc] init];
        vc.model = self.model.sr[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    } else if (self.type == BookTypeNewconcept) {
        
    } else if (self.type == BookTypeHearsay) {
        ListenTestViewController *vc = [[ListenTestViewController alloc] init];
        if (self.type == BookTypeWrittenExam) {
            vc.model = self.model.sr[indexPath.row];
        } else {
            vc.model = self.model.hsr[indexPath.row];
        }
        vc.type = self.type;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        
    }
    
}
@end
